<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .input-box {
            margin: 20px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid grey;
            outline: none;
            padding-left: 20px;
            border-radius: 10px;
        }

        .input-box span {
            text-align: center;
            color: cornflowerblue;
            width: 60px;
        }

        .list {
            margin: 10px;
        }

        .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 15px;
            background-color: coral;
            color: white;
        }

        .list .task.finished {
            background-color: blue;
        }

        .empty {
            text-align: center;
            margin: 50px 0;
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="请输入任务名称" v-model="inputText">
            <span @click="add">添加</span>
        </div>
        <div class="list">
            <!-- :class=" `task ${item.status?'finished':''}` " -->
            <div  :class="{ task: true, finished: item.status }"  v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" @click="changeStatus(item)">
                <div class="name">{{item.name}}</div>
                <span class="btn">删除</span>
            </div>
        </div>
        <div class="empty" v-show="tasklist.length == 0">任务列表为空!</div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        //挂载到dom节点上
        el: '#app',
        data: {
            //保存输入框中的值
            inputText: '',
            tasklist:
                [

                ]
        },
        methods: {
            //添加任务的方法
            add() {
                //输入框里有内容，我们才添加任务
                if (this.inputText) {
                    //构造一个任务对象
                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false };
                    //添加任务对象到任务列表所在的数组里
                    this.tasklist.push(obj);
                    //每执行完一次我们需要清空输入框
                    this.inputText = ''
                }
            },
            //更改任务状态, item是当前点击的任务对象
            changeStatus(item) {
                item.status = !item.status;
            }
        }
    })
</script>